<template>
  <div class="signatureManagement-box">
    <el-container class="signature-box">
      <!-- 搜索 -->
      <el-header height="auto">
        <SearchForm
          label-width="80px"
          :data="wholesaledata"
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading='submitLoading'
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="tableLoading"
        :data="dataList"
        height="400"
        :columns="temrplateColumns"
      >
        <!-- 序号 -->
        <template
          slot="default-num"
          slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
        <!-- 授权状态 -->
        <template
          slot="default-remove"
          slot-scope="scope">
          <span>{{ scope.row.remove === true ? '已授权' : '未授权' }}</span>
        </template>
        <!-- 操作 -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            size="small"
            :disabled="scope.row.status !== 1"
            @click.stop="toSignature(scope.row)"
            type="text text-primary">授权印章</el-button>
          <el-button
            size="small"
            v-if="scope.row.status === 1"
            @click.stop="toStop(scope.row,0)"
            type="text text-primary">停用</el-button>
          <el-button
            size="small"
            v-if="scope.row.status !== 1"
            @click.stop="toStop(scope.row,1)"
            type="text text-primary">启用</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
      <!-- 授权印章 -->
      <el-dialog
        title="授权印章"
        :visible.sync="isSignature"
        width="800px">
        <el-editable
          ref="sealTable"
          :data="AllSignature"
          height="320"
          :columns="sealColumns"
          @selection-change="handleSelectionChange"
        />
        <div class="dialog-btn">
          <el-button
            size="small"
            @click.stop="tocancel">
            <i class="el-icon-circle-close"/>
            取消</el-button>
          <el-button
            size="small"
            @click.stop="toSure"
            type="primary">
            <i class="el-icon-circle-check"/>
            确定</el-button>
        </div>
      </el-dialog>
    </el-container>
  </div>
</template>
<script>
import { wholesaledata, temrplateColumns, sealColumns } from './const'
import {
  signatureList,
  allSignature,
  addSignature,
  stopSignatureList,
  addSignatureInfo
} from '@const/interfaceCenterApi'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
export default {
  components: { ElEditable, SearchForm },
  data () {
    return {
      isSignature: false, // 是否显示添加印章弹框
      AllSignature: [], // 所有印章
      signatureList: [], // 选中的印章
      userId: null, // 员工id

      wholesaledata, // 搜索字段
      searchData: {}, // 搜索的值
      submitLoading: false,
      tableLoading: false,
      dataList: [], // 列表数据
      temrplateColumns, // 列表表头

      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数

      sealColumns,
      multipleSelection: [] // 授权签章多选
    }
  },
  created () {
    this.getDataList() // 获取列表数据
    this.getTemplateType() // 获取所有印章
  },
  methods: {
    // 反写已经授权签章
    toggleSelection (rows) {
      let $sealTable = this.$refs['sealTable'].$children[0]
      if (rows) {
        rows.forEach(row => {
          $sealTable.toggleRowSelection(row, true)
        })
      }
    },
    // 选项变化
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 授权盖章参数处理
    postLastparams () {
      this.AllSignature.forEach(outItem => {
        outItem.isRemove = 0
        this.multipleSelection.forEach(item => {
          if (outItem.name === item.name) {
            outItem.isRemove = 1
          }
        })
      })
      let params = this.AllSignature.map(item => {
        let newItem = {}
        newItem.companyId = this.$store.state.companyId
        newItem.name = item.name
        newItem.userId = this.userId
        newItem.isRemove = item.isRemove
        return newItem
      })
      return params || []
    },
    // 确定
    toSure () {
      let Signaturedata = this.postLastparams()
      this.$axios({
        method: 'post',
        url: addSignature,
        data: Signaturedata
      })
        .then(res => {
          if (res.data.payload.code === '1') {
            this.$message.success('操作成功')
            this.isSignature = false
          } else {
            this.$message.warning('操作失败')
          }
          
        }).catch(() => {})
          .finally(res => {
          this.getDataList()
        })
    },
    // 取消
    tocancel () {
      this.isSignature = false
      this.signatureList = []
    },
    // 获取所有印章
    getTemplateType () {
      this.$axios
        .$post(allSignature)
        .then(res => {
          let { payload } = res
          this.AllSignature = payload || []
        })
        .finally(res => {})
    },
    // 获取列表数据
    getDataList () {
      this.submitLoading = true
      this.tableLoading = true
      this.$axios.$post(signatureList, {
        ...this.searchData,
        page: this.curPage,
        pageSize: this.pageSize
      }).then(res => {
        let { content, totalElements } = res.payload
        this.dataList = content || []
        this.totalElements = totalElements || 0
      })
        .finally(res => {
          this.tableLoading = false
          this.submitLoading = false
        })
    },
    //   停用启用
    toStop (e, type) {
      let text = ''
      if (type !== 0) {
        // 启用
        text = '启用'
      } else {
        // 停用
        text = '停用'
      }
      this.$confirm('此操作将' + text + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios({
            method: 'post',
            url: stopSignatureList + '/' + e.id
          })
            .then(res => {
              if (res.data.code === '0') {
                this.$message({
                  message: text + '成功!',
                  type: 'success'
                })
                this.getDataList()
              }
            })
            .finally(res => {})
        })
        .catch(() => {})
    },
    // 获取授权印章列表
    toSignature (e) {
      this.userId = e.userId
      this.isSignature = true
      this.$axios({
        method: 'post',
        url: addSignatureInfo + '/' + e.userId
      })
        .then(res => {
          let payload = res.data.payload || []
          this.signatureList = payload.map(item => {
            return item.sealName
          }) || []
          let rows = this.getToggleRows()
          this.toggleSelection(rows)
        })
        .catch(() => {})
        .finally(res => {})
    },
    // 处理需要反显的数据
    getToggleRows () {
      let rows = []
      if (this.signatureList.length) {
        this.signatureList.forEach(item => {
          this.AllSignature.forEach(inner => {
            if (item === inner.name) {
              rows.push(inner)
            }
          })
        })
      }
      return rows
    },
    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.getDataList()
    },
    // 搜索功能-点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchForm(condition)
    },
    // 页码更改
    handleSizeChange (size) {
      this.curPage = 1
      this.pageSize = size
      this.getDataList()
    },
    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getDataList()
    }
  }
}
</script>
<style lang="less">
.signatureManagement-box {
  .signature-box {
    height: 100%;
    overflow: auto;

    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }

  .dialog-btn {
    padding-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .el-transfer-panel {
    width: 250px;
  }
}
</style>
